/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ufs.dcomp.mgemeos.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import ufs.dcomp.mgemeos.negocio.Material;

/**
 *
 * @author Luiz Marcus
 * @madificacao 14/02/2011
 */
public class MaterialDAO implements interface_dao.IMaterial {

    private static String inserir = "insert into exposicao (link, tipo, idExposicao) values (?,?,?)";
    private static String obter_materias_id = "select * from material where idExposicao=?";

    @Override
    public boolean inserirMaterial(Material material) {
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;

        try {
            pst = con.prepareStatement(inserir);
            pst.setString(1, material.getLink());
            pst.setString(2, material.getTipo().toString());
            pst.setInt(3, material.getIdExposicao());

            pst.execute();
            pst.close();
            con.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public ArrayList<Material> obter_todos(int id) {
        Material expo = null;
        ArrayList<Material> lista_materiais = new ArrayList<Material>();
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;
        try {
            pst = con.prepareStatement(obter_materias_id);
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {

                expo = new Material();
                expo.setIdMaterial(rs.getInt(1));
                expo.setLink(rs.getString(2));
                expo.setTipo(rs.getString(3));
                expo.setIdExposicao(rs.getInt(4));

                lista_materiais.add(expo);
            }
            rs.close();
            pst.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lista_materiais;
    }
}
